package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LStatisticalDTO;
import com.learn.core.service.*;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LStatisticalController
 * @date 2021.04.19 11:10
 * @describe 统计分析
 */
@Api("统计分析")
@RestController
@RequestMapping("/manager/lStatistic")
public class LStatisticalController {
    @Autowired
    private ILCourseService ilCourseService;
    @Autowired
    private ILEnrollService ilEnrollService;
    @Autowired
    private ILCourseSortService ilCourseSortService;
    @Autowired
    private ILExamService ilExamService;
    @Autowired
    private ILExamUserService ilExamUserService;
    @Autowired
    private ILThemeService ilThemeService;
    @Autowired
    private ILTeacherService ilTeacherService;
    @Autowired
    private IUserDetailsGatherService iUserDetailsGatherService;
    @Autowired
    private ILOrganizationService ilOrganizationService;
    @Autowired
    private ILExamSortService ilExamSortService;
    @Autowired
    private ILExamineService ilExamineService;
    @Autowired
    private ILExamineQuestionService ilExamineQuestionService;
    @Autowired
    private ILExamineUserService ilExamineUserService;
    @Autowired
    private ILCompetitionUserService ilCompetitionUserService;
    @Autowired
    private ILCompetitionService ilCompetitionService;
    @Autowired
    private ILCompetitionPaperService ilCompetitionPaperService;

    //学习统计
    @ApiOperation(value = "学习统计")
    @UserLoginToken()
    @PostMapping("/learnList")
    public PageResult<LCourse> learnList(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            if (lStatisticalDTO==null||lStatisticalDTO.getPage()==null||lStatisticalDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lStatisticalDTO.getPage(),lStatisticalDTO.getRows());
            QueryWrapper<LCourse> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lStatisticalDTO.getName()))
                queryWrapper.like("course_name",lStatisticalDTO.getName());
            if(StringUtils.isNotEmpty(lStatisticalDTO.getStartTime()))
                queryWrapper.gt("release_time",lStatisticalDTO.getStartTime());
            if(StringUtils.isNotEmpty(lStatisticalDTO.getEndTime()))
                queryWrapper.le("release_time",lStatisticalDTO.getEndTime());
            Page<LCourse> lCourses = (Page<LCourse>)ilCourseService.list(queryWrapper);
            for (LCourse lCourse: lCourses) {
                QueryWrapper<LEnroll> enrollNumWrapper = new QueryWrapper<>();
                enrollNumWrapper.eq("course_id",lCourse.getCourseId());
                int enrollNum = ilEnrollService.count(enrollNumWrapper);
                lCourse.setEnrollNum(enrollNum);
                QueryWrapper<LEnroll> studyNumWrapper = new QueryWrapper<>();
                studyNumWrapper.eq("course_id",lCourse.getCourseId());
                studyNumWrapper.ne("stutes","0");
                int studyNum = ilEnrollService.count(studyNumWrapper);
                lCourse.setStudyNum(studyNum);
                QueryWrapper<LEnroll> completeNumWrapper = new QueryWrapper<>();
                completeNumWrapper.eq("course_id",lCourse.getCourseId());
                List inlist = new ArrayList();
                inlist.add(3);inlist.add(4);
                completeNumWrapper.in("stutes",inlist);
                int completeNum = ilEnrollService.count(completeNumWrapper);
                lCourse.setCompleteNum(completeNum);
                if(enrollNum > 0){
                    BigDecimal all = new BigDecimal(enrollNum);
                    BigDecimal studyBig = new BigDecimal(studyNum).multiply(new BigDecimal(100));
                    String studyRate = studyBig.divide(all,2,BigDecimal.ROUND_HALF_UP).toString();
                    lCourse.setStudyRate(studyRate);
                    BigDecimal completeBig = new BigDecimal(completeNum).multiply(new BigDecimal(100));
                    String completeRate = completeBig.divide(all,2,BigDecimal.ROUND_HALF_UP).toString();
                    lCourse.setCompleteRate(completeRate);
                }else{
                    lCourse.setStudyRate("0");
                    lCourse.setCompleteRate("0");
                }
                LCourseSort lCourseSort = ilCourseSortService.getById(lCourse.getCourseSortId());
                if(null != lCourseSort)
                    lCourse.setCourseSortName(lCourseSort.getCourseSortName());
                LTheme lTheme = ilThemeService.getById(lCourse.getCourseTheme());
                if(null != lTheme)
                    lCourse.setCourseThemeName(lTheme.getThemeName());
                LOrganization lOrganization = ilOrganizationService.getById(lCourse.getCourseCompany());
                if(null != lOrganization)
                    lCourse.setCourseCompanyName(lOrganization.getOrganizationName());
            }
            return new PageResult(lCourses.getTotal(), lCourses, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //学习统计详情
    @ApiOperation(value = "学习统计详情")
    @UserLoginToken
    @PostMapping("/learnInfo")
    public CommonResult<Object> learnInfo(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            QueryWrapper<LCourse> queryWrapper = new QueryWrapper<LCourse>();
            queryWrapper.eq("course_id",lStatisticalDTO.getId());
            if(StringUtils.isNotEmpty(lStatisticalDTO.getStartTime()))
                queryWrapper.gt("release_time",lStatisticalDTO.getStartTime());
            if(StringUtils.isNotEmpty(lStatisticalDTO.getEndTime()))
                queryWrapper.le("release_time",lStatisticalDTO.getEndTime());
            LCourse lCourse = ilCourseService.getOne(queryWrapper);
            QueryWrapper<LEnroll> enrollNumWrapper = new QueryWrapper<>();
            enrollNumWrapper.eq("course_id",lCourse.getCourseId());
            int enrollNum = ilEnrollService.count(enrollNumWrapper);
            lCourse.setEnrollNum(enrollNum);
            QueryWrapper<LEnroll> studyNumWrapper = new QueryWrapper<>();
            studyNumWrapper.eq("course_id",lCourse.getCourseId());
            studyNumWrapper.eq("stutes","1");
            int studyNum = ilEnrollService.count(studyNumWrapper);
            lCourse.setStudyNum(studyNum);
            QueryWrapper<LEnroll> completeNumWrapper = new QueryWrapper<>();
            completeNumWrapper.eq("course_id",lCourse.getCourseId());
            completeNumWrapper.eq("stutes","2");
            int completeNum = ilEnrollService.count(completeNumWrapper);
            lCourse.setCompleteNum(completeNum);
            if(enrollNum > 0){
                BigDecimal all = new BigDecimal(enrollNum);
                BigDecimal studyBig = new BigDecimal(studyNum).multiply(new BigDecimal(100));
                String studyRate = studyBig.divide(all,2,BigDecimal.ROUND_HALF_UP).toString();
                lCourse.setStudyRate(studyRate);
                BigDecimal completeBig = new BigDecimal(completeNum).multiply(new BigDecimal(100));
                String completeRate = completeBig.divide(all,2,BigDecimal.ROUND_HALF_UP).toString();
                lCourse.setCompleteRate(completeRate);
            }else{
                lCourse.setStudyRate("0");
                lCourse.setCompleteRate("0");
            }
            LCourseSort lCourseSort = ilCourseSortService.getById(lCourse.getCourseSortId());
            if(null != lCourseSort)
                lCourse.setCourseSortName(lCourseSort.getCourseSortName());
            LTheme lTheme = ilThemeService.getById(lCourse.getCourseTheme());
            if(null != lTheme)
                lCourse.setThemeName(lTheme.getThemeName());
            LTeacher lTeacher = ilTeacherService.getById(lCourse.getTeacher());
            if(null != lTeacher)
                lCourse.setTeacherName(lTeacher.getTeacherName());
            LOrganization lOrganization = ilOrganizationService.getById(lCourse.getCourseCompany());
            if(null != lOrganization)
                lCourse.setCourseCompanyName(lOrganization.getOrganizationName());
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lCourse);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //查询学员列表
    @ApiOperation(value = "查询学员列表")
    @UserLoginToken
    @PostMapping("/learnUserList")
    public PageResult<LEnroll> learnUserList(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            if (lStatisticalDTO==null||lStatisticalDTO.getPage()==null||lStatisticalDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lStatisticalDTO.getPage(),lStatisticalDTO.getRows());
            QueryWrapper<LEnroll> queryWrapper = new QueryWrapper<LEnroll>();
            if(StringUtils.isNotEmpty(lStatisticalDTO.getId()))
                queryWrapper.eq("course_id",lStatisticalDTO.getId());
            Page<LEnroll> lEnrolls = (Page<LEnroll>) ilEnrollService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lEnrolls)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LEnroll lEnroll : lEnrolls) {
                QueryWrapper<UserDetailsGather> gatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
                gatherQueryWrapper.eq("user_id" ,lEnroll.getUserId());
                UserDetailsGather lUser = iUserDetailsGatherService.getOne(gatherQueryWrapper);
                if(null != lUser) {
                    lEnroll.setUserName(lUser.getName());
                    LOrganization lOrganization = ilOrganizationService.getById(lUser.getOrganizationId());
                    if(null != lOrganization)
                        lEnroll.setOrganizationName(lOrganization.getOrganizationName());
                }
            }
            return new PageResult(lEnrolls.getTotal(), lEnrolls, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //考试统计
    @ApiOperation(value = "考试统计")
    @UserLoginToken()
    @PostMapping("/examList")
    public PageResult<LExam> examList(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            if (lStatisticalDTO==null||lStatisticalDTO.getPage()==null||lStatisticalDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lStatisticalDTO.getPage(),lStatisticalDTO.getRows());
            QueryWrapper<LExam> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lStatisticalDTO.getName()))
                queryWrapper.like("exam_name",lStatisticalDTO.getName());
            if(StringUtils.isNotEmpty(lStatisticalDTO.getStartTime()))
                queryWrapper.gt("release_time",lStatisticalDTO.getStartTime());
            if(StringUtils.isNotEmpty(lStatisticalDTO.getEndTime()))
                queryWrapper.le("release_time",lStatisticalDTO.getEndTime());
            Page<LExam> lExams = (Page<LExam>)ilExamService.list(queryWrapper);
            for (LExam lExam: lExams) {
                QueryWrapper<LExamUser> enrollNumWrapper = new QueryWrapper<>();
                enrollNumWrapper.eq("exam_id",lExam.getExamId());
                int enrollNum = ilExamUserService.count(enrollNumWrapper);
                lExam.setEnrollNum(enrollNum);
                QueryWrapper<LExamUser> examNumWrapper = new QueryWrapper<>();
                examNumWrapper.eq("exam_id",lExam.getExamId());
                examNumWrapper.ne("exam_user_state","0");
                int examNum = ilExamUserService.count(examNumWrapper);
                lExam.setExamNum(examNum);
                QueryWrapper<LExamUser> completeNumWrapper = new QueryWrapper<>();
                completeNumWrapper.eq("exam_id",lExam.getExamId());
                if(StringUtils.isNotEmpty(lExam.getPassingMark()))
                    completeNumWrapper.ge("user_score",Integer.parseInt(lExam.getPassingMark()));
                else
                    completeNumWrapper.ge("user_score",0);
                int completeNum = ilExamUserService.count(completeNumWrapper);
                lExam.setCompleteNum(completeNum);
                if(enrollNum > 0){
                    BigDecimal all = new BigDecimal(enrollNum);
                    BigDecimal studyBig = new BigDecimal(completeNum).multiply(new BigDecimal(100));
                    String completeRate = studyBig.divide(all,2,BigDecimal.ROUND_HALF_UP).toString();
                    lExam.setCompleteRate(completeRate);
                }
                QueryWrapper<LExamUser> averageWrapper = new QueryWrapper<>();
                averageWrapper.select("round(avg(user_score),2) as average");
                averageWrapper.eq("exam_id",lExam.getExamId());
                LExamUser average = ilExamUserService.getOne(averageWrapper);
                if(null != average){
                    lExam.setAverage(average.getAverage());
                }else{
                    lExam.setAverage("0");
                }
                QueryWrapper<LExamUser> maxScoresWrapper = new QueryWrapper<>();
                maxScoresWrapper.select("MAX(user_score) as maxScores");
                maxScoresWrapper.eq("exam_id",lExam.getExamId());
                LExamUser maxScores = ilExamUserService.getOne(maxScoresWrapper);
                if(null != maxScores){
                    lExam.setMaxScores(maxScores.getMaxScores());
                }else{
                    lExam.setMaxScores("0");
                }
                QueryWrapper<LExamUser> minScoresWrapper = new QueryWrapper<>();
                minScoresWrapper.select("MIN(user_score) as minScores");
                minScoresWrapper.eq("exam_id",lExam.getExamId());
                LExamUser minScores = ilExamUserService.getOne(minScoresWrapper);
                if(null != minScores){
                    lExam.setMinScores(minScores.getMinScores());
                }else{
                    lExam.setMinScores("0");
                }
                LOrganization lOrganization = ilOrganizationService.getById(lExam.getOrganizationId());
                if(null != lOrganization)
                    lExam.setOrganizationName(lOrganization.getOrganizationName());
                LExamSort lExamSort = ilExamSortService.getById(lExam.getExamSortId());
                if(null != lExamSort)
                    lExam.setExamSortName(lExamSort.getExamSortName());
            }
            return new PageResult(lExams.getTotal(), lExams, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //考试详情
    @ApiOperation(value = "考试详情")
    @UserLoginToken
    @PostMapping("/examInfo")
    public CommonResult<Object> examInfo(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            QueryWrapper<LExam> queryWrapper = new QueryWrapper<LExam>();
            queryWrapper.eq("exam_id",lStatisticalDTO.getId());
            LExam lExam = ilExamService.getOne(queryWrapper);

            LOrganization lOrganization = ilOrganizationService.getById(lExam.getOrganizationId());
            if(null != lOrganization)
                lExam.setOrganizationName(lOrganization.getOrganizationName());
            LExamSort lExamSort = ilExamSortService.getById(lExam.getExamSortId());
            if(null != lExamSort)
                lExam.setExamSortName(lExamSort.getExamSortName());
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lExam);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //考试统计报表
    @ApiOperation(value = "考试统计报表")
    @UserLoginToken
    @PostMapping("/examReport")
    public CommonResult<Object> examReport(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            Map<String,Object> map = new HashMap<>();
            QueryWrapper<LExam> queryWrapper = new QueryWrapper<LExam>();
            queryWrapper.eq("exam_id",lStatisticalDTO.getId());
            LExam lExam = ilExamService.getOne(queryWrapper);
            if(null != lExam){
                map.put("passingMark",lExam.getPassingMark());
                QueryWrapper<LExamUser> enrollNumWrapper = new QueryWrapper<>();
                enrollNumWrapper.eq("exam_id",lExam.getExamId());
                int enrollNum = ilExamUserService.count(enrollNumWrapper);
                map.put("enrollNum",enrollNum);
                QueryWrapper<LExamUser> examNumWrapper = new QueryWrapper<>();
                examNumWrapper.eq("exam_id",lExam.getExamId());
                examNumWrapper.ne("exam_user_state","0");
                int examNum = ilExamUserService.count(examNumWrapper);
                map.put("examNum",examNum);
                QueryWrapper<LExamUser> completeNumWrapper = new QueryWrapper<>();
                completeNumWrapper.eq("exam_id",lExam.getExamId());
                completeNumWrapper.ge("user_score",Integer.parseInt(lExam.getPassingMark()));
                int completeNum = ilExamUserService.count(completeNumWrapper);
                map.put("completeNum",completeNum);
                if(enrollNum > 0){
                    BigDecimal all = new BigDecimal(enrollNum);
                    BigDecimal studyBig = new BigDecimal(completeNum).multiply(new BigDecimal(100));
                    String completeRate = studyBig.divide(all,2,BigDecimal.ROUND_HALF_UP).toString();
                    map.put("completeRate",completeRate);
                }
                QueryWrapper<LExamUser> averageWrapper = new QueryWrapper<>();
                averageWrapper.select("round(avg(user_score),2) as average");
                averageWrapper.eq("exam_id",lExam.getExamId());
                LExamUser average = ilExamUserService.getOne(averageWrapper);
                if(null != average){
                    map.put("average",average.getAverage());
                }else{
                    map.put("average",0);
                }
                QueryWrapper<LExamUser> maxScoresWrapper = new QueryWrapper<>();
                maxScoresWrapper.select("MAX(user_score) as maxScores");
                maxScoresWrapper.eq("exam_id",lExam.getExamId());
                LExamUser maxScores = ilExamUserService.getOne(maxScoresWrapper);
                if(null != maxScores){
                    map.put("maxScores",maxScores.getMaxScores());
                }else{
                    map.put("maxScores",0);
                }
                QueryWrapper<LExamUser> minScoresWrapper = new QueryWrapper<>();
                minScoresWrapper.select("MIN(user_score) as minScores");
                minScoresWrapper.eq("exam_id",lExam.getExamId());
                LExamUser minScores = ilExamUserService.getOne(minScoresWrapper);
                if(null != minScores){
                    map.put("minScores",minScores.getMinScores());
                }else{
                    map.put("minScores",0);
                }
                List<Map<String,Object>> list = ilExamUserService.selectNum(lExam.getExamId(),lStatisticalDTO.getNum());
                map.put("reportList",list);

                QueryWrapper<LExamUser> lExamUserQueryWrapper = new QueryWrapper<>();
                lExamUserQueryWrapper.eq("exam_id",lExam.getExamId());
                List<LExamUser> lExamUserList = ilExamUserService.list(lExamUserQueryWrapper);
                for (LExamUser lExamUser : lExamUserList) {
                    QueryWrapper<UserDetailsGather> gatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
                    gatherQueryWrapper.eq("user_id" ,lExamUser.getUserId());
                    UserDetailsGather lUser = iUserDetailsGatherService.getOne(gatherQueryWrapper);
                    if(null != lUser)
                        lExamUser.setUserName(lUser.getName());
                }
                map.put("examUserList",lExamUserList);
            }
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //调查问卷列表
    @ApiOperation(value = "调查问卷列表")
    @UserLoginToken
    @PostMapping("/examineList")
    public PageResult<LEnroll> examineList(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            if (lStatisticalDTO==null||lStatisticalDTO.getPage()==null||lStatisticalDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lStatisticalDTO.getPage(),lStatisticalDTO.getRows());
            QueryWrapper<LExamine> queryWrapper = new QueryWrapper<LExamine>();
            if(StringUtils.isNotEmpty(lStatisticalDTO.getName()))
                queryWrapper.like("examine_name",lStatisticalDTO.getName());
            Page<LExamine> lExamines = (Page<LExamine>) ilExamineService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lExamines)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LExamine lExamine : lExamines) {
                LOrganization lOrganization = ilOrganizationService.getById(lExamine.getOrganizationId());
                if(null != lOrganization)
                    lExamine.setOrganizationName(lOrganization.getOrganizationName());
            }
            return new PageResult(lExamines.getTotal(), lExamines, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //调查问卷详情
    @ApiOperation(value = "调查问卷详情")
    @UserLoginToken
    @PostMapping("/examineInfo")
    public CommonResult<Object> examineInfo(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            LExamine lExamine = ilExamineService.getById(lStatisticalDTO.getId());
            if(null != lExamine){
                LOrganization lOrganization = ilOrganizationService.getById(lExamine.getOrganizationId());
                if(null != lOrganization)
                    lExamine.setOrganizationName(lOrganization.getOrganizationName());
            }
            Map<String,Object> map = new HashMap<>();
            map.put("lExamine",lExamine);
            if(null != lExamine){
                QueryWrapper<LExamineQuestion> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("examine_id", lExamine.getExamineId());
                List<LExamineQuestion> lexamines = ilExamineQuestionService.list(queryWrapper);
                List<Map<String,Object>> list = new ArrayList<>();
                for (LExamineQuestion lExamineQuestion : lexamines) {
                    Map<String,Object> examineMap = new HashMap<>();
                    examineMap.put("examineQuestionId",lExamineQuestion.getExamineQuestionId());
                    examineMap.put("examineQuestionTitle",lExamineQuestion.getExamineQuestionTitle());
                    String option = lExamineQuestion.getExamineQuestionOption();
                    String [] optionArray = option.split("\\|");
                    List<Map<String,Object>> strList = new ArrayList();
                    for (String str: optionArray) {
                        Map<String,Object> strMap = new HashMap<>();
                        QueryWrapper<LExamineUser> lExamineUserQueryWrapper = new QueryWrapper<>();
                        lExamineUserQueryWrapper.eq("examine_id",lExamine.getExamineId());
                        lExamineUserQueryWrapper.like("answer",lExamineQuestion.getExamineQuestionId()+":"+str);
                        int count = ilExamineUserService.count(lExamineUserQueryWrapper);
                        strMap.put("option",str);
                        strMap.put("optionCount",count);
                        strList.add(strMap);
                    }
                    examineMap.put("optionList",strList);
                    list.add(examineMap);
                }
                map.put("questionList",list);
            }
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //竞赛统计列表
    @ApiOperation(value = "竞赛统计列表")
    @UserLoginToken
    @PostMapping("/competitionList")
    public PageResult<LCompetition> competitionList(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            if (lStatisticalDTO==null||lStatisticalDTO.getPage()==null||lStatisticalDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lStatisticalDTO.getPage(),lStatisticalDTO.getRows());
            QueryWrapper<LCompetition> queryWrapper = new QueryWrapper<LCompetition>();
            if(StringUtils.isNotEmpty(lStatisticalDTO.getName()))
                queryWrapper.like("competition_name",lStatisticalDTO.getName());
            Page<LCompetition> lCompetitions = (Page<LCompetition>) ilCompetitionService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lCompetitions)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            return new PageResult(lCompetitions.getTotal(), lCompetitions, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //竞赛统计详情
    @ApiOperation(value = "竞赛统计详情")
    @UserLoginToken
    @PostMapping("/competitionInfo")
    public CommonResult<Object> competitionInfo(@RequestBody LStatisticalDTO lStatisticalDTO){
        try {
            LCompetition lCompetition = ilCompetitionService.getById(lStatisticalDTO.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("lCompetition",lCompetition);
            if(null != lCompetition){
                QueryWrapper<LCompetitionUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("competition_id", lCompetition.getCompetitionId());
                List<LCompetitionUser> lCompetitionUsers = ilCompetitionUserService.list(queryWrapper);
                for (LCompetitionUser lCompetitionUser : lCompetitionUsers) {
                    QueryWrapper<UserDetailsGather> detailsGatherQueryWrapper = new QueryWrapper<>();
                    detailsGatherQueryWrapper.eq("user_id", lCompetitionUser.getUserId());
                    UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(detailsGatherQueryWrapper);
                    if(null != userDetailsGather)
                        lCompetitionUser.setUserName(userDetailsGather.getName());
                }
                map.put("lCompetitionUser",lCompetitionUsers);
            }
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }
}
